using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace Banshee.Base.Winforms.Widgets
{
    public partial class SeekSlider : TrackBar
    {
        private uint timeout;
        private uint timeout_delay = 500;
        private bool can_seek;
        private bool raise_seek_requested;
        private bool can_set_value;
        private IPalette _palette;
        public event EventHandler SeekRequested;
        public event EventHandler DurationChanged;
        public SeekSlider()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer, true);
            UpdateStyles();
            //this.BackColor = Color.FromArgb(83,83,83);
            TickStyle = TickStyle.None;
            this.AutoSize = false;
            _palette = KryptonManager.CurrentGlobalPalette;
            if (_palette != null)
            {
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(_palette_PalettePaint);
                Color back = _palette.GetBackColor1(PaletteBackStyle.PanelClient, PaletteState.Normal);
                ViewColor = back;
            }
            KryptonManager.GlobalPaletteChanged += new EventHandler(KryptonManager_GlobalPaletteChanged);
            this.ValueChanged += new EventHandler(SeekSlider_ValueChanged);
            MouseDown += new MouseEventHandler(SeekSlider_MouseDown);
            MouseUp += new MouseEventHandler(SeekSlider_MouseUp);
            
        }

        void KryptonManager_GlobalPaletteChanged(object sender, EventArgs e)
        {
            this.Refresh();
        }

        void _palette_PalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            this.Refresh();
        }
        public Color ViewColor
        {
            get { return this.BackColor; }
            set { this.BackColor = value; }
        }
        void SeekSlider_MouseUp(object sender, MouseEventArgs e)
        {
            can_set_value = true;
            OnSeekRequested();
        }

        void SeekSlider_MouseDown(object sender, MouseEventArgs e)
        {
            can_set_value = false;

        }
        void SeekSlider_ValueChanged(object sender, EventArgs e)
        {
            if (raise_seek_requested)
            {
                OnSeekRequested();
            }
            //base.OnValueChanged(e);
        }
        public long SeekValue
        {
            get { return (long)Value; }
            set
            {
                if (!can_set_value)
                {
                    return;
                }

                raise_seek_requested = false;

                if (value > Duration)
                {
                    Duration = value;
                }

                Value = (int)value;
                raise_seek_requested = true;
            }
        }

        public double Duration
        {
            get { return Maximum; }
            set
            {
                Maximum = (int)value;
                EventHandler handler = DurationChanged;
                if (handler != null)
                {
                    handler(this, new EventArgs());
                }
            }
        }
        private bool OnSeekRequested()
        {
            if (raise_seek_requested)
            {
                EventHandler handler = SeekRequested;
                if (handler != null)
                {
                    handler(this, new EventArgs());
                }
            }

            timeout = 0;
            return false;
        }
        public void SetIdle()
        {
            SeekValue = 0;
            Duration = 0;
        }

        public uint SeekRequestDelay
        {
            get { return timeout_delay; }
            set { timeout_delay = value; }
        }

        public bool CanSeek
        {
            get { return can_seek; }
            set
            {
                can_seek = value;
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            
            base.OnPaint(e);
            Color back = _palette.GetBackColor1(PaletteBackStyle.PanelClient, PaletteState.Normal);
            ViewColor = back;
        }
    }

}
